<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 密钥设置
 * Class Bonus
 * @package 
 */
class Secret extends Api
{

	protected $noNeedLogin = [];
	protected $noNeedRight = '*';

	/**
	 * 更改 - 支付密码
	 */
	public function change_pay_pass()
	{
		$uid = $this->auth->id;
		$typs = $this->request->post("typs");  // 修改方式 ：1 输入原密码修改  2 忘记密码修改  
		$old_pass = $this->request->post("oldPass"); // 原密码
		$new_pass = $this->request->post("newPass"); // 新密码
		$flag = false;
		$reg = "0003";
		if (!empty($typs) && !empty($new_pass)) {
			$reg = "0014";
			$table = "user_pay_pass";
			$find = Db::name($table)->where("agid = {$uid}")->find();
			if ($find) {
				$check =  true;
				if ($typs == 1) {
					$check = false;
					$reg = "0015";
					if (!empty($old_pass) && is_numeric($old_pass)  && strlen($old_pass) == 6) {
						$reg = "0011";
						if (md5($old_pass) === $find["passwords"]) {
							$reg = "0016";
							if (md5($old_pass) !== md5($new_pass)) {
								$check = true;
							}
						}
					}
				}
				if ($check) {
					$reg = "0202";
					$option["passwords"] = md5($new_pass);
					$option["modify_time"] = time();
					$ret = Db::name($table)->where("id = " . $find["id"])->update($option);
					if ($ret) {
						$flag = true;
					}
				}
			}
		}
		$regMsg = Config("msg");
		if ($flag === false) {
			$this->error($regMsg[$reg], 0);
		}
		$this->success($regMsg["0200"], 1);
	}

	/**
	 * 设置 - 支付密码
	 */
	public function pay_pass()
	{
		$uid = $this->auth->id;
		$pass = $this->request->post("password", '', "trim");
		if ($pass) {
			if (is_numeric($pass) &&  strlen($pass) == 6) {
				$option = [];
				$table = "user_pay_pass";
				$option = ["agid" => $uid];
				$check = Db::name($table)
					->where($option)
					->count();
				if (!$check) {
					$option["passwords"] = md5($pass);
					$option["create_time"] = time();
					$req = Db::name($table)->insert($option);
					if ($req) {
						$this->success("支付密码设置成功", 1);
					} else {
						$this->error("支付密码设置失败，请重新输入！！！");
					}
				} else {
					$this->error("已设置支付密码，无需重复！！！");
				}
			} else {
				$this->error("密码必须是6位数字密码！！！");
			}
		} else {
			$this->error("密码必须是6位数字密码！！！");
		}
	}

	/**
	 * 放款 - 确认支付密码
	 */
	public function pass_confirm_pass()
	{
		$pass = $this->request->post("password", '', "trim");
		if ($pass) {
			$uid = $this->auth->id;
			$tab = "user_pay_pass";
			$where = "agid = " . $uid;
			$finds_pass = Db::name($tab)
				->where($where)
				->field("passwords")
				->find();
			if ($finds_pass) {
				if ($finds_pass["passwords"] == md5($pass)) {
					$this->success("正常");
				} else {
					$this->error("密码错误，重新输入");
				}
			} else {
				$this->error("支付密码未设置，请先设置密码！！！");
			}
		} else {
			$this->error("密码错误，重新输入");
		}
	}

	/**
	 * 检测 是否设置支付密码
	 */
	public function check_pay_pass()
	{
		$uid = $this->auth->id;
		$limit_ids = [2, 5, 6, 10, 12];
		if (in_array($uid, $limit_ids)) {
			$this->error("禁止操作！！！");
		} else {
			$table = "user_pay_pass";
			$option = ["agid" => $uid];
			$check = Db::name($table)
				->where($option)
				->count();
			$ver = 0;
			if ($check) {
				$ver = 1;
			}
			$this->success("success", $ver);
		}
	}
}
